package com.futvan.z.framework.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.z;
import com.futvan.z.system.zvcode.z_vcode;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送类
 * @author 4223947@qq.com
 *
 */
public class SmsUtil {
    /**
     * 发送手机验证码短信
     * @param tel
     * @param code
     * @return
     */
    public static Result Send(String tel,String code) {
        //通过阿里云短信接口发送
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("code", code);
        return Send("模板",tel,map);
    }
    /**
     * 发送手机验证码短信
     * @param TemplateCode
     * @param tel
     * @return
     */
    public static Result SendForTemplate(String TemplateCode,String tel) {
        return Send(TemplateCode,tel,null);
    }

    /**
     * 通用发送短信
     * @param TemplateCode 短信模板
     * @param Tel 手机号
     * @param Param HashMap参数
     * @return Result
     */
    private static Result Send(String TemplateCode,String Tel,HashMap<String,String> Param) {
        Result result = new Result();
        String AccessKeyId = z.sp.get("sms_key_id");
        String AccessKeySecret = z.sp.get("sms_key_secret");
        String SignName = z.sp.get("sms_sign_name");
        if(z.isNotNull(AccessKeyId) && z.isNotNull(AccessKeySecret) && z.isNotNull(SignName)){
            String TemplateParam = JsonUtil.getJson(Param);
            try {
                DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
                IAcsClient client = new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.setMethod(MethodType.POST);
                request.setDomain("dysmsapi.aliyuncs.com");
                request.setVersion("2017-05-25");
                request.setAction("SendSms");
                request.putQueryParameter("RegionId", "cn-hangzhou");
                request.putQueryParameter("PhoneNumbers", Tel);
                request.putQueryParameter("SignName", SignName);
                request.putQueryParameter("TemplateCode", TemplateCode);
                request.putQueryParameter("TemplateParam", TemplateParam);
                CommonResponse response = client.getCommonResponse(request);
                Map mapdata = (Map)JsonUtil.getObject(response.getData(), Map.class);
                if ("OK".equals(mapdata.get("Code"))) {
                    result.setCode(Code.SUCCESS);
                    result.setMsg("发送成功");
                    z.Log("发送短信成功|" + Tel + "|" + TemplateParam);
                } else {
                    result.setCode(Code.ERROR);
                    result.setMsg(String.valueOf(mapdata.get("Message")));
                    z.Log("发送短信失败|" + Tel + "|" + TemplateParam + "|" + String.valueOf(mapdata.get("Message")));
                }
            } catch (Exception e) {
                result.setCode(Code.ERROR);
                result.setMsg(e.getMessage());
            }
        }else{
            result.setError("发送短信异常|未配置短信平台相关参数");
        }

        return result;
    }

    /**
     * 添加发送短信记录
     * @param name
     * @param info
     * @param tel
     */
    private static void addSendLog(String tel,String name,String info) {
        z_vcode vc = new z_vcode();
        vc.setZid(z.newZid("z_vcode"));
        vc.setName(name);
        vc.setVcode(info);
        vc.setTel(tel);
        vc.setEndtime(DateUtil.DateAdd(Calendar.MINUTE, 5));//当前时间加5分钟
        z.dbs.get("z").insert("z_vcode_insert", vc);
    }
}
